﻿using NfsBLL;
using NfsCommon.Util;
using NfsCommon.WeChartHelper;
using NfsModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MobileWeb.Controllers
{
    public class WechartShareController : Controller
    {
        //
        // GET: /WechartShare/
        public ActionResult Index()
        {
            int id = int.Parse(CookieHelper.GetDecodeCookie("nfs_UserId"));
            AgencyBLL agbll = new AgencyBLL();
            agency model = agbll.GetModel_account(id);
            string share_state ="0";
            if (model != null)
            { 
                share_state=model.share_state!=null?model.share_state.ToString():"0";
            }else{
                share_state = "0";
            }
            ViewData["share_state"] = share_state;
            ViewData["category_id"] =model!=null?model.category_id:0;
            return View(model);
        }
      

        public string getConfig(string url, string createNonceStr)
        {
            GetTokenAndTinket tt = new GetTokenAndTinket();
            string wxConfig = tt.GetConfig(url, createNonceStr);
            return wxConfig;
        }
        #region 绑定下拉框类别
        public JsonResult GetLevelName()
        {
            try
            {
                int id = int.Parse(CookieHelper.GetDecodeCookie("nfs_UserId"));
                AgencyBLL agbll = new AgencyBLL();
                agency model = agbll.GetModel_account(id);
                level le_model=new level();
                 LevelBLL bll = new LevelBLL();
                 if (model != null)
                 {
                     le_model = bll.GetModel((int)model.level_id);


                     if (le_model != null)
                     {
                         List<level> list = bll.GetModelList(" valid=1 and code like '%" + le_model.code + "%'");
                         return Json(new { MessageCode = "0", list = list }, JsonRequestBehavior.AllowGet);

                     }
                     else {
                         return Json(new { MessageCode = "100" }, JsonRequestBehavior.AllowGet);
                     }
                 }
                 else {
                     return Json(new { MessageCode = "100" }, JsonRequestBehavior.AllowGet);
                 
                 }

              
            }
            catch (Exception)
            {
                return Json(new { MessageCode = "200" }, JsonRequestBehavior.AllowGet);
            }

        }
        #endregion
        #region 分享等级操作
        [HttpPost]
        public JsonResult opeq_share()
        {
            try
            {
                int id = int.Parse(CookieHelper.GetDecodeCookie("nfs_UserId"));
                int level = Request["level"] != "" ? int.Parse(Request["level"]) : 0;
                int category_id = Request["category_id"] != "" ? int.Parse(Request["category_id"]) : 0;
                string  level_name = Request["level_name"] != "" ? Request["level_name"] :"";
                string nonceStr = createNonceStr(); ;
                ShareBLL bll = new ShareBLL();
                share model = new share { level_id = level,category_id=category_id,level_name = level_name,parent_id = id, nonceStr = nonceStr, valid = 1, state = 0, time = DateTime.Now };
                bool flag=bll.Add(model);
                if (flag)
                {
                    return Json(new { MessageCode = "0", nonceStr = nonceStr }, JsonRequestBehavior.AllowGet);

                }
                else {
                    return Json(new { MessageCode = "100" }, JsonRequestBehavior.AllowGet);
                }

            }
            catch (Exception)
            {
                return Json(new { MessageCode = "200" }, JsonRequestBehavior.AllowGet);
            }

        }
        #endregion

        /// <summary>
        /// 创建随机字符串
        /// </summary>
        /// <returns>随机字符串</returns>
        private string createNonceStr()
        {
            int length = 16;
            string chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
            string str = "";
            Random rad = new Random();
            for (int i = 0; i < length; i++)
            {
                str += chars.Substring(rad.Next(0, chars.Length - 1), 1);
            }
            return str;

        }

	}
}